<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('资源使用情况列表')" />
</head>
<body class="gray-bg">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="post-form">
                    <div class="select-list">
                        <ul>
                            <li>
                                节点：
                                <select name="nodeCode" th:with="nodes=${@node.getCacheNodeInfoAll()}">
                                    <option value="">所有</option>
                                    <option th:each="node:${nodes}" th:title="${node.ip}" th:value="${node.nodeCode}" th:text="${node.hostname}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-warning" onclick="$.table.exportExcel()">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>

            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var nodeStatusDatas = [[${@dict.getType('survive_status')}]];
        var prefix = ctx + "system/nodeUsed";

        $(function() {
            queryList();
        });

        function queryList() {
            var options = {
                url: prefix + "/list",
                exportUrl: prefix + "/export",
                sortName: "nodeCode",
                modalName: "节点",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '编号',
                    sortable: true,
                    visible: false
                },
                {
                    field: 'nodeCode',
                    title: '节点编号',
                    sortable: true
                },
                {
                    field: 'hostname',
                    title: '主机名',
                    sortable: true
                },
                {
                    field: 'ip',
                    title: 'IP地址'
                },
                {
                    field: 'detectionTime',
                    title: '更新时间'
                },
                {
                    field: 'diskTotal',
                    title: '总磁盘大小',
					visible: false,
					formatter: function (value, row, index) {
						return bytesToSize(value);
					}
                },
                {
                    field: 'diskUsed',
                    title: '已使用磁盘大小',
                    visible: false,
                    formatter: function (value, row, index) {
                        return bytesToSize(value);
                    }
                },
                {
                    field: 'diskUsage',
                    title: '磁盘使用率(%)',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return pointToPercent(value);
                    }
                },
                {
                    field: 'cpuNum',
                    title: 'CPU核心数',
                    visible: false
                },
                {
                    field: 'cpuUsed',
                    title: 'CPU使用率(%)',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return pointToPercent(value);
                    }
                },
                {
                    field: 'memoryTotal',
                    title: '内存总量',
                    visible: false,
                    formatter: function (value, row, index) {
                        return bytesToSize(value);
                    }
                },
                {
                    field: 'memoryUsed',
                    title: '已用内存',
                    visible: false,
                    formatter: function (value, row, index) {
                        return bytesToSize(value);
                    }
                },
                {
                    field: 'memoryUsed',
                    title: '内存使用率(%)',
                    sortable: true,
                    formatter: function (value, row, index) {
                        let number = value / row.memoryTotal;
                        return pointToPercent(number);
                    }
                },
                {
                    field: 'networkName',
                    title: '网口名称',
                    visible: false,
                },
                {
                    field: 'packetsReceiveAmount',
                    title: '数据包接收量',
                    visible: false
                },
                {
                    field: 'packetsSendAmount',
                    title: '数据包发送量',
                    visible: false
                },
                {
                    field: 'uploadBps',
                    title: '上行/s',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return bytesToSize(value);
                    }
                },
                {
                    field: 'downloadBps',
                    title: '下行/s',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return bytesToSize(value);
                    }
                }]
            };
            $.table.init(options);
        }

        /* 字节转换 */
		function bytesToSize(bytes) {
			if (bytes == 0) return '0 B';
			var k = 1024;
			sizes = ['B','KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
			i = Math.floor(Math.log(bytes) / Math.log(k));
			var num = bytes / Math.pow(k, i);
			return num.toPrecision(3) + ' ' + sizes[i];
			//return (bytes / Math.pow(k, i)) + ' ' + sizes[i];
			//toPrecision(3) 后面保留一位小数，如1.0GB //return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
		}

		/* 百分比转换 */
        function pointToPercent(point) {
            return Number(point * 100).toFixed(2);
        }

    </script>
</body>
</html>